const {
  $util,
} = getApp();
const MAX_LENGTH = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    path: 'information',
    hasMore: true,
    loaded: false,
    list: [],
    nav: [{ title: '文章', id: 0, path: 'information' }, { title: '帖子', id: 1, path: 'group' }],
    abnorData: {
      imgSrc: '/common/static/images/abnor-16.png',
      text: '你还没有发表过评论',
    },
    imgUrl: $util.getImgUrl(),
    idx: 0,
  },
  onShow() {
    this.setData({
      page: 1,
    }, () => {
      this.getList();
    });

    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
    
  },
  onLoad() {
    
  },
  changeNav(e) {
    this.setData({
      page: 1,
      path: e.detail.obj.path,
      list: [],
      loaded: false,
      idx: e.detail.idx,
    }, () => {
      this.getList();
    });
  },
  getList() {
    const { page } = this.data;
    $util.request({
      url: `entry/wxapp/${this.data.path}.comment.mycomments`,
      data: {
        page,
      },
      success: (res) => {
        let list = res.data;
        if (page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          hasMore: !(page > 1 && res.data.length < MAX_LENGTH),
          loaded: true,
        });
      },
    });
  },
  // 删除评论
  deteleComment(e) {
    const { list } = this.data;
    const { idx } = e.currentTarget.dataset;
    const { id } = list[idx];
    let url = '';
    if (this.data.path === 'information') {
      url = 'entry/wxapp/information.comment.delselfcomments';
    } else if (this.data.path === 'group') {
      url = 'entry/wxapp/group.comment.myrecorddelete';
    }
    wx.showModal({
      content: '确认要删除该评论吗？',
      success: (res) => {
        if (res.confirm) {
          $util.request({
            url,
            data: {
              id,
            },
            success: () => {
              list.splice(idx, 1);
              this.setData({
                list,
              });
            },
          });
        }
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.hasMore) return;
    this.setData({
      page: this.data.page + 1,
    }, () => {
      this.getList();
    });
  },
});
